package com.xnx3.wangmarket.plugin.innerDown.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;
import com.xnx3.wangmarket.plugin.innerDown.entity.InnerDownBranch;
import com.xnx3.wangmarket.plugin.innerDown.generateCache.Branch;
import com.xnx3.wangmarket.plugin.kefu_domain.entity.Kefu;
import com.xnx3.wangmarket.plugin.kefu_domain.bean.MQBean;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.func.AttachmentFile;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.j2ee.vo.UploadFileVO;
import com.xnx3.wangmarket.admin.entity.News;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.AliyunLog;
import com.xnx3.wangmarket.domain.mq.DomainMQ;

/**
 * 下载系统，部门管理
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/innerDown/admin/branch/")
public class AdminBranchInnerDownPluginController extends BasePluginController{
	@Resource
	private SqlService sqlService;
	
	/**
	 * 部门列表
	 */
	@RequestMapping("/list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
	    Sql sql = new Sql(request);
	    /*
	     * 设置可搜索字段。这里填写的跟user表的字段名对应。只有这里配置了的字段，才会有效。这里没有配置，则不会进行筛选
	     * 具体规则可参考： http://note.youdao.com/noteshare?id=3ccef2de6a5cda01f95f832b02e356d0&sub=D53E681BBFF04822977C7CFBF8827863
	     */
	    sql.setSearchColumn(new String[]{"name"});
	    sql.appendWhere("isdelete = "+InnerDownBranch.ISDELETE_NORMAL);
	    //查询user数据表的记录总条数。 传入的user：数据表的名字为user
	    int count = sqlService.count("plugin_innerDown_branch", sql.getWhere());
	    //创建分页，并设定每页显示15条
	    Page page = new Page(count, 30, request);
	    //创建查询语句，只有SELECT、FROM，原生sql查询。其他的where、limit等会自动拼接
	    sql.setSelectFromAndPage("SELECT * FROM plugin_innerdown_branch", page);
	    //因只查询的一个表，所以可以将查询结果转化为实体类，用List接收。
	    List<InnerDownBranch> list = sqlService.findBySql(sql, InnerDownBranch.class);
	    
	    //将展示的列表数据记录传到页面以供显示
	    model.addAttribute("list", list);
	    //将分页信息传到页面以供显示底部分页
	    model.addAttribute("page", page);
	    return "plugin/innerDown/admin/branch/list";
	}
	

	/**
	 * 创建、修改页面
	 */
	@RequestMapping(value="branch${url.suffix}")
	public String branch(HttpServletRequest request, Model model,
			@RequestParam(value = "id", required = false , defaultValue="0") int id){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		
		InnerDownBranch branch = null;
		if(id > 0){
			//修改
			branch = sqlService.findById(InnerDownBranch.class, id);
			if(branch == null){
				return error(model, "要修改的信息不存在");
			}
		}else{
			//新增
			branch = new InnerDownBranch();
			branch.setIsdelete(InnerDownBranch.ISDELETE_NORMAL);
		}
		
		model.addAttribute("branch", branch);
		return "plugin/innerDown/admin/branch/branch";
	}
	
	/**
	 * 创建、修改页面提交保存
	 */
	@RequestMapping(value="branchSave${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO branchSave(InnerDownBranch branchInput, HttpServletRequest request){
		if(!haveSuperAdminAuth()){
			return error("无权使用");
		}
		
		InnerDownBranch branch = null;
		
		if(branchInput.getId() != null && branchInput.getId() > 0){
			//修改
			branch = sqlService.findById(InnerDownBranch.class, branchInput.getId());
			if(branch == null){
				return error("要修改的信息不存在");
			}
		}else{
			//新增
			branch = new InnerDownBranch();
			branch.setIsdelete(InnerDownBranch.ISDELETE_NORMAL);
		}
		branch.setName(filter(branchInput.getName()));
		sqlService.save(branch);
		
		generateJsCache();
		
		return success();
	}
	
	

	/**
	 * 删除
	 * @return
	 */
	@RequestMapping(value="delete${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(HttpServletRequest request,
			@RequestParam(value = "id", required = false , defaultValue="0") int id){
		if(!haveSuperAdminAuth()){
			return error("无权使用");
		}
		
		if(id > 0){
			sqlService.executeSql("DELETE FROM plugin_innerdown_branch WHERE id="+id);
			generateJsCache();
			return success();
		}else{
			return error("请传入要删除的部门id");
		}
	}
	
	//生成部门的js缓存
	private void generateJsCache(){
		List<InnerDownBranch> list = com.xnx3.j2ee.func.Sql.getSqlService().findBySqlQuery("SELECT * FROM plugin_innerdown_branch WHERE isdelete = "+InnerDownBranch.ISDELETE_NORMAL, InnerDownBranch.class);
		new Branch().branchid(list);
	}
}
